package initialize

import (
	"context"
	"fmt"
	"gitee.com/youbeiwuhuan/gin-practice/gin-gorm2/global"
	"gitee.com/youbeiwuhuan/gin-practice/gin-gorm2/global/config"
	gorm_v2 "gitee.com/youbeiwuhuan/gin-practice/gin-gorm2/tools/gormv2"
	"gitee.com/youbeiwuhuan/gin-practice/gin-gorm2/tools/logger"
	"sync"
)

var (
	initGorm2Once sync.Once
)

func InitGorm2() {
	initGorm2Once.Do(func() {
		initGorm2_()
	})
}

func initGorm2_() {
	log := logger.New(context.Background(), GetZap())

	db, err := gorm_v2.GetSqlDriver(config.TotalConfig.Gorm2, GetZap())

	if nil != err {
		panic(fmt.Sprintf("initGorm2_ err:%v", err))
	} else {
		log.Info("------initGorm2_---success---------\n")
	}
	global.GormDb = db

}
